// ----> 废弃

// 创建下拉选项
function addOption(cmb, name, val){
    // cmb 是下拉框对象
     console.log("获取到的" + val);
    var option = document.createElement("option");
    cmb.options.add(option);
    option.innerHTML = name;
    //option.value = value;
    $(option).val(val);
}

// 清除下拉选项
function ClearOption(cmb){
    for (let i = 0; i<=cmb.length +1; i++){
        cmb.options.remove(cmb[i]);
    }
}

// 获取项目信息
var ProjectInit = function(_cmbProject){
    var cmbProject = document.getElementById(_cmbProject);
    // 获取项目信息
    function getProjectInfo(){
        $.get("/project/get_projects/",{},function(resp){
            $('selectProject').selectpicker('refresh')
            if (resp.status === 10200){
                // console.log(resp.data);
                let dataList = resp.data;
                for(let i =0; i<dataList.length; i++){
                    addOption(cmbProject, dataList[i].name, dataList[i].id);
                }

            }

            else{
                window.alert(resp.msg)
            }
                });
            }
    getProjectInfo();
}

// 获取模块信息
var ModuleInit = function(_cmbModule,pid){
    var cmbModule = document.getElementById(_cmbModule)
    console.log("当前的操作"+cmbModule)
    // 调用项目所属模块接口
    function getModuleInfo(){
        $.post("/module/get_modules/",{"pid":pid},function(resp){
            if (resp.status ===10200){
                 ClearOption(cmbModule)
                for (let i =0; i< resp.data.length; i++){
                        addOption(cmbModule,resp.data[i].name,resp.data[i].id)
                    }
                $('#selectModule').selectpicker('refresh')
            }
            else{
                window.alert(resp.msg)
            }
        })
    }
    getModuleInfo();
}



// 获取用例信息用于编辑用例
var TestCaseInit = function(){
    //  获取URL的4种方法
    // var url_1 = window.location.href
    // var url_2 = self.location.href
    // var url_3 = document.URL
    var url_4 = document.location
    console.log("url_4:" + url_4)
    var case_id = url_4.pathname.split("/")[3]
    console.log(case_id)

    $.post("/case_manage/get_case_info/",
        {
            case_id : case_id
        },
        function(resp,status){
            console.log(resp.data)
            document.querySelector('#req_url').value = resp.data.url
            document.querySelector('#case_name').value = resp.data.name
            document.querySelector('#header').value = resp.data.request_header
            document.querySelector('#parameter').value = resp.data.parameters
            document.querySelector('#assert').value = resp.data.expected_value
            document.querySelector('#json_extract').value = resp.data.json_extract
            document.querySelector('#regular_expression').value = resp.data.regular_expression
            document.querySelector('#regular_extract').value = resp.data.regular_extract
            document.querySelector("#result").value = resp.data.response
            // 给参数类型赋值
            if(resp.data.params_type == "form-data"){
                    document.querySelector('#form').setAttribute("checked","")
                }
            else if(resp.data.params_type == "json"){contains
                    document.querySelector("#json").setAttribute("checked","")
                }
            // 给断言类型赋值
            if (resp.data.assert_type == "equal"){
                    document.querySelector('#equal').setAttribute("checked","")
                }
            else if (resp.data.assert_type == "contains"){
                    document.querySelector('#contains').setAttribute("checked","")
                }
           // 给请求方法赋值
            let request_method_options = document.querySelectorAll("#req_method > option")
            console.log("请求方法"+request_method_options)
            for(let j = 0; j<request_method_options.length;j++){
                if(request_method_options[j].value == resp.data.request_method){
                    request_method_options[j].selected = true
                     // document.querySelectorAll("#req_method > option")[j].value = resp.data.request_method
                     document.querySelectorAll(".filter-option-inner-inner")[1].innerText = resp.data.request_method.toUpperCase( )
                }
            }

           // 设置已选项目下拉值
            let options = document.querySelectorAll("#selectProject > option")
            console.log("ttt",options.length)
            for(let i = 0; i < options.length; i++){
                let v = options[i].value
                console.log("V:" + v)
                if(v == resp.data.project_id){
                    options[i].selected = true
                    let text = options[i].text
                    console.log("所属项目名称：" +  text)
                    document.querySelectorAll(".filter-option-inner-inner")[3].innerText = resp.data.project_name
                }
            }
        }
    );
}